#include "stdafx.h"

#include <cassert>

#include "PFOrderControllor.h"
#include "PFSectypeControllor.h"
#include "PFPositionControllor.h"
#include "PFCurrencyControllor.h"

#include "PFControllorList.h"

std::unordered_map<PFControllorType, PFControllor *> PFControllorList::m_controllors;

class PFControllorLoader
{
public:
	PFControllorLoader(PFControllorList * controllorList)
		: m_pControllorList(controllorList)
	{

	}

	/*
	PFControllorPool : static PFControllor * PFQuoteControllor
	PFControllorPool : static PFControllor * PFSecTypeControllor
	PFControllorPool : static PFControllor * PFPositionControllor
	PFControllorPool : static PFControllor * PFOrderControllor
	PFControllorPool : static PFControllor * PFCurrencyControllor
	PFControllorPool : static PFControllor * PFAssetControllor
	*/
	PFControllor * Load(PFControllorType type);

	PFControllorList * m_pControllorList = nullptr;
};

PFControllor * PFControllorLoader::Load(PFControllorType type)
{
	PFControllor * controllor = nullptr;
	switch (type)
	{
	case PFControllorType::ECT_Cash:
	case PFControllorType::ECT_Quote:
	case PFControllorType::ECT_Position:
		controllor = new PFPositionControllor;
		break;
	case PFControllorType::ECT_Order:
		controllor = new PFOrderControllor;
		break;
	case PFControllorType::ECT_Currency:
		controllor = new PFCurrencyControllor;
		break;
	case PFControllorType::ECT_SecType:
		controllor = new PFSectypeControllor;
		break;
	}

	m_pControllorList->SetControllor(type, controllor);

	assert(controllor && "controllor not exist");

	return controllor;
}

PFControllorList::~PFControllorList()
{
	
}

std::unordered_map<PFControllorType, PFControllor *> PFControllorList::GetList()
{
	return m_controllors;
}

void PFControllorList::CleanList()
{
	auto iter = m_controllors.begin();
	while (iter != m_controllors.end())
	{
		delete iter->second;
		m_controllors.erase(iter);

		iter = m_controllors.begin();
	}
}

PFControllor * PFControllorList::GetControllor(PFControllorType type)
{
	PFControllorLoader loader(this);
	return loader.Load(type);
}

void PFControllorList::SetControllor(PFControllorType type, PFControllor * controllor)
{
	auto iter = m_controllors.find(type);
	if (iter == m_controllors.end())
	{
		m_controllors.insert({ type, controllor });
	}
	else
	{
		iter->second = controllor;
	}
}

void PFControllorList::RemoveControllor(PFControllorType type)
{
	auto iter = m_controllors.find(type);
	if (iter != m_controllors.end())
	{
		m_controllors.erase(iter);
	}
}
